using System;
using UnityEngine;
using UnityEngine.UI;

public static  class UIExtension
{
    public static void AutoAnchor(this RectTransform rect)
    {
        if (rect)
        {
            rect.anchorMin = Vector2.zero;
			rect.anchorMax = Vector2.one;
        }
    }

    public static void SetAlpha(this Graphic rect, float alpha)
    {
        if (rect)
        {
            Color c = rect.color;
            c.a = alpha;
            rect.color = c;
        }
    }

    public static void PivotAlignWithSprite(this Image image)
    {
        var sprite = image.sprite;
        if (sprite == null) return;
        var spritePivot = sprite.pivot;
        var spriteRect = sprite.rect;
        image.rectTransform.pivot = new Vector2(spritePivot.x/spriteRect.width, spritePivot.y/spriteRect.height);
    }
}